package com.SNQ.project.English_speaking_learning_system.controller.admin;

import com.SNQ.project.English_speaking_learning_system.common.result.PageResult;
import com.SNQ.project.English_speaking_learning_system.common.result.Result;
import com.SNQ.project.English_speaking_learning_system.pojo.dto.AdminDTO;
import com.SNQ.project.English_speaking_learning_system.pojo.dto.AdminLoginDTO;
import com.SNQ.project.English_speaking_learning_system.pojo.dto.AdminPageQueryDTO;
import com.SNQ.project.English_speaking_learning_system.pojo.entity.Admin;
import com.SNQ.project.English_speaking_learning_system.pojo.vo.AdminLoginVO;
import com.SNQ.project.English_speaking_learning_system.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/admin")
@Slf4j
@Api(tags = "管理员相关接口")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 登录
     * @param adminLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "管理员登录")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO) {//传进来一个json对象
        //返回数据是什么类型，泛型就是什么类型
        log.info("管理员登录登录：{}", adminLoginDTO);
        //日志级别：等级由低到高:debug<info<warn<Error<Fatal
        Admin admin = adminService.login(adminLoginDTO);

        AdminLoginVO adminLoginVO = AdminLoginVO.builder()
                .id(admin.getId())
                .userName(admin.getUsername())
                .name(admin.getName())
                .build();

        return Result.success(adminLoginVO);
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("管理员退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增管理员
     * @param adminDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增管理员")
    public Result save(@RequestBody AdminDTO adminDTO){
        log.info("新增管理员：{}",adminDTO);
        adminService.save(adminDTO);
        return Result.success();
    }

    /**
     * 管理员分页查询
     * @param adminPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("管理员分页查询")
    public Result<PageResult> page(AdminPageQueryDTO adminPageQueryDTO){
        log.info("管理员分页查询，参数为：{}", adminPageQueryDTO);
        PageResult pageResult = adminService.pageQuery(adminPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 启动禁用管理员账号
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用员工账号")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("启用禁用管理员账号：{},{}",status,id);
        adminService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 根据id查询管理员信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工信息")
    public Result<Admin> getById(@PathVariable Long id){
        Admin admin = adminService.getById(id);
        return Result.success(admin);
    }

    /**
     * 编辑管理员信息
     * @param adminDTO
     * @return
     */
    @PutMapping
    @ApiOperation("编辑管理员信息")
    public Result update(@RequestBody AdminDTO adminDTO){
        log.info("编辑管理员信息：{}", adminDTO);
        adminService.update(adminDTO);
        return Result.success();
    }
}

